package com.qzdsoft.erpcloud.service.sys.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qzdsoft.erpcloud.domain.act.CenterCompany;
import com.qzdsoft.erpcloud.domain.basic.BasicUser;
import com.qzdsoft.erpcloud.domain.sys.LoginUser;
import com.qzdsoft.erpcloud.mapper.act.CenterCompanyMapper;
import com.qzdsoft.erpcloud.mapper.basic.BasicUserMapper;
import com.qzdsoft.erpcloud.mapper.sys.SysLoginMapper;
import com.qzdsoft.erpcloud.service.sys.SysLoginService;
import com.qzdsoft.erpcloud.vo.sys.RegisterInfo;
import com.qzdsoft.utils.SysUtils;
import com.qzdsoft.utils.common.CommonStatusEnum;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.TypeInfo;
import com.qzdsoft.vo.sys.LoginUserType;
import com.qzdsoft.vo.sys.SysUserRole;
import com.qzdsoft.vo.sys.SysUserStatusEnum;
import com.qzdsoft.vo.sys.SysUserType;

/**
 * Created by Admin
 */
@Service
public class SysLoginServiceImpl implements SysLoginService {

    @Autowired
    private SysLoginMapper sysLoginMapper;

    @Autowired
    private CenterCompanyMapper centerCompanyMapper;
    
    @Autowired
    private BasicUserMapper basicUserMapper;
    
    @Override
    public LoginUser selectForLogin(String name, String password) {
        LoginUser user = sysLoginMapper.selectForUser(name, password);
        List<TypeInfo> companys = new ArrayList<>();
        if(user!=null) {
            //如果是系统用户查询所有公司  
            if(LoginUserType.SYSUSER_CODE.equals(user.getLoginType().toString())) {
                List<CenterCompany> centerCompanys = centerCompanyMapper.selectAll();
                for(CenterCompany c:centerCompanys) {
                    companys.add(new TypeInfo(c.getId()+"",c.getName()));
                }
            //如果是代理商 查询代理商代理的所有公司    
            }else if(LoginUserType.AGENT_CODE.equals(user.getLoginType().toString())){
                List<CenterCompany> centerCompanys = centerCompanyMapper.selectAll();
                for(CenterCompany c:centerCompanys) {
                    if(c.getRecommendUid().equals(user.getId())) {
                        companys.add(new TypeInfo(c.getId()+"",c.getName()));
                    }
                }
            //查询自己公司
            }else {
                CenterCompany company = centerCompanyMapper.selectByPrimaryKey(user.getCompanyId());
                companys.add(new TypeInfo(company.getId()+"",company.getName()));
            }
            user.setCompanys(companys);
        }
        return user;
        
        
    }
    @Override
    @Transactional
    public Response<String> registerSave(RegisterInfo registerInfo)
    {   BasicUser basic1 = new BasicUser();
        basic1.setPhone(registerInfo.getPhone());
        int size = basicUserMapper.selectCount(basic1);
        if(size>0) {
            return new Response<String>(ResultEnum.ERROR,"该手机号已注册！");
        }
        BasicUser basic2 = new BasicUser();
        basic2.setEmail(registerInfo.getEmail());
        size = basicUserMapper.selectCount(basic2);
        if(size>0) {
            return new Response<String>(ResultEnum.ERROR,"该邮箱已注册！！");
        }
        
        CenterCompany centerCompany = new CenterCompany();
        centerCompany.setName(registerInfo.getCompanyName());
        centerCompany.setEmail(registerInfo.getEmail());
        centerCompany.setAddress(registerInfo.getAddress());
        centerCompany.setRecommendUid(registerInfo.getReferee());
        centerCompany.setStatus(SysUserStatusEnum.using.getKey());
        centerCompany.setContactUser(registerInfo.getContact());
        centerCompany.setContactPhone(registerInfo.getPhone());
        centerCompany.setSmRate(1000);
        centerCompany.setVoiceRete(800);
        
        Date date = new Date();
        centerCompany.setCtime(date); //创建时间
        Calendar   calendar   =   new   GregorianCalendar(); 
        calendar.setTime(date); 
        calendar.add(calendar.YEAR, 1);
        date=calendar.getTime(); 
        centerCompany.setEndTime(date); //有效截止时间
        
        centerCompanyMapper.insert(centerCompany);
        
        BasicUser record = new BasicUser();
        record.setName(registerInfo.getContact());
        record.setPhone(registerInfo.getPhone());
        record.setEmail(registerInfo.getEmail());
        record.setPassword(SysUtils.string2MD5(registerInfo.getPassword()));
        record.setRoleId(SysUserRole.role_two.getKey());  //功能角色设置公司基本帐户
        record.setCompanyId(centerCompany.getId());
        record.setStatus(SysUserStatusEnum.using.getKey()+"");
        record.setType(SysUserType.role_two.getKey()+"");  //设置为公司用户
        record.setCtime(new Date());
        
        basicUserMapper.insert(record);
        
        
        return new Response<String>(ResultEnum.SUCCESS);
    }
}
